<!-- 
 Copyright 2009 University of  Washington
This code is released under the GPL version 3 - with a copy of the license included
@author Maya Hao Li
 -->
<%@page import="seedpod.webapp.view.InstanceRenderer"%>
<%@page
	import="seedpod.model.custom.SeedpodUser"%>
<%@page import="seedpod.model.SeedpodDO"%>
<%@page import="seedpod.webapp.Seedpod"%>
<%@page import="seedpod.rdb.PManager"%>
<%@page import="seedpod.webapp.LimsSession"%>
<%@page
	import="seedpod.webapp.view.htmlwidget.Button"%>
<%@page import="seedpod.webapp.view.Page"%>
<%@page
	import="seedpod.exceptions.SeedpodException"%>
<%@page import="seedpod.kb2db.SlotMap" %>
<%@page import="java.util.Collection" %>	
<%@page import="seedpod.util.Util" %>

<%
	Page spPage = new Page();
	spPage.setTitle("create new instnace");
	spPage.addJsLibrary("../javascript/lib/scriptaculous/src/effects.js");
	spPage.addJsLibrary("../javascript/lib/scriptaculous/src/scriptaculous.js");

	LimsSession spSession = new LimsSession(request);
	SeedpodUser user = spSession.getUser();
	
	String clsName = request.getParameter("clsName");
	
	
	String newInstanceDivID = Util.getSeedpodInternalId(clsName, 0);
	out.println( "<div id=\""+newInstanceDivID+ "\" + class=\"newInstance\">");
	
	if ( !user.canEditData() ) {
		out.println("You don't have rights to write to the database. Sorry. Please contact your lab manager for assistance.");
	} else if (clsName == null ){
		out.println("<script>location.replace('createInstance.jsp')</script>");
	}
	else	{
		PManager manager;
		SeedpodDO spObject;
		String actionNewInstanceUrl = Seedpod.getAbsoluteUrl("lims/actionNewInstance?clsName="+clsName);

		
		try {
			manager = spSession.getDataManager();
			spObject = (SeedpodDO) manager.createObject(clsName); 

			
			if (spObject == null )
				out.println( "Can not create new instance form for requested class: " + clsName);
			else {
					spObject.makeTransient();

					// get spObject data for the new Instance from request URL string. 
					// This is important for prepopulating data for create similar or create objects with relationships. 
					Collection<SlotMap> attributes = spObject.getMetaSlots();
					for (SlotMap a : attributes ) {
						String  value = request.getParameter( a.getName());
						
						if ( value != null ) {
							System.err.println("request param set to spObject " + a.getName() + " = " + value ); 
							spObject.setAttributeValue(a, value);
						}
						else 
							System.err.println( "request  : " + a.getName() ); 
					}
					
					spSession.cacheSeedpodObject( spObject 	);
					InstanceRenderer instanceViewer = new InstanceRenderer(spObject);
					String newInstanceTable = instanceViewer.renderCreateForm();
					
					if ( newInstanceTable != null  ) {
						out.println("<div class=\"sectionCommand\">New "+ clsName + "</div>");
						out.println("<form action=\""+actionNewInstanceUrl+"\" method=\"post\">");
						
						out.println( newInstanceTable );
						
						Button btnCreate = new Button("createButton", "Create");
						btnCreate.setCssClass("button");
						btnCreate.setType(Button.TYPE_SUBMIT);
						
						Button btnCancel = new Button("cancelButton", "Cancel");
						btnCancel.setCssClass("button");
						btnCancel.setRedirect( spSession.popRequestedURL() );
						
						out.println(btnCreate.render());
						out.println(btnCancel.render());
		
						out.println("</form>");
					}
				 else
					out.println("No attributes defined for class " + clsName);
			}
		}
		catch (SeedpodException e) {
			throw new SeedpodException(
			"Cannot access database while trying to get object");
		}
	}
	
	out.println("</div>"); // end new instance form

%>

